drivers=sfm3003 sfm3019
sample-projects=sfm3019-arduino
clean_drivers=$(foreach d, $(drivers), clean_$(d))
release_drivers=$(foreach d, $(drivers), release/$(d))
release_sample_projects=$(foreach s, $(sample-projects), release/$(s))

.PHONY: FORCE all $(release_drivers) $(clean_drivers) style-check style-fix

all: prepare $(drivers)

prepare: sfm-common/sfm_git_version.c

$(drivers): prepare
	cd $@ && $(MAKE) $(MFLAGS)

sfm-common/sfm_git_version.c: FORCE
	git describe --always --dirty | \
		awk 'BEGIN \
		{print "/* THIS FILE IS AUTOGENERATED */"} \
		{print "#include \"sfm_git_version.h\""} \
		{print "const char* SFM_DRV_VERSION_STR = \"" $$0"\";"} \
		END {}' > $@ || echo "Can't update version, not a git repository"


$(release_drivers): sfm-common/sfm_git_version.c
	export rel=$@ && \
	export driver=$${rel#release/} && \
	export tag="$$(git describe --always --dirty)" && \
	export pkgname="$${driver}-$${tag}" && \
	export pkgdir="release/$${pkgname}" && \
	rm -rf "$${pkgdir}" && mkdir -p "$${pkgdir}" && \
	cp -r embedded-common/hw_i2c/ "$${pkgdir}" && \
	cp -r embedded-common/sw_i2c/ "$${pkgdir}" && \
	cp embedded-common/sensirion_arch_config.h "$${pkgdir}" && \
	cp embedded-common/sensirion_common.c "$${pkgdir}" && \
	cp embedded-common/sensirion_common.h "$${pkgdir}" && \
	cp embedded-common/sensirion_i2c.h "$${pkgdir}" && \
	cp -r sfm-common/* "$${pkgdir}" && \
	cp -r $${driver}/* "$${pkgdir}" && \
	cp CHANGELOG.md LICENSE "$${pkgdir}" && \
	echo 'sensirion_common_dir = .' >> $${pkgdir}/user_config.inc && \
	echo 'sfm_common_dir = .' >> $${pkgdir}/user_config.inc && \
	echo "$${driver}_dir = ." >> $${pkgdir}/user_config.inc && \
	cd "$${pkgdir}" && $(MAKE) $(MFLAGS) && $(MAKE) clean $(MFLAGS) && cd - && \
	cd release && zip -r "$${pkgname}.zip" "$${pkgname}" && cd - && \
	ln -sfn $${pkgname} $@

$(release_sample_projects):
	export rel=$@ && \
	export sample_project=$${rel#release/} && \
	export tag="$$(git describe --always --dirty)" && \
	export pkgname="$${sample_project}-sample-project-$${tag}" && \
	export pkgdir="release/$${pkgname}" && \
	rm -rf "$${pkgdir}" && mkdir -p "$${pkgdir}" && \
	sample-projects/$${sample_project}/prepare_release.sh  "$${pkgdir}" && \
	cd release && zip -r "$${pkgname}.zip" "$${pkgname}" && cd - && \
	ln -sfn $${pkgname} release/$${sample_project}

release: clean $(release_drivers) $(release_sample_projects)

$(clean_drivers):
	export rel=$@ && \
	export driver=$${rel#clean_} && \
	cd $${driver} && $(MAKE) clean $(MFLAGS) && cd -

clean: $(clean_drivers)
	rm -rf release
	$(RM) sfm-common/sfm_git_version.c

style-fix:
	@if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \
	then \
		echo "Refusing to run on dirty git state. Commit your changes first."; \
		exit 1; \
	fi; \
	git ls-files | grep -e '\.\(c\|h\|cpp\)$$' | xargs clang-format-6.0 -i -style=file;

style-check: style-fix
	@if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \
	then \
		echo "Style check failed:"; \
		git diff; \
		git checkout -f; \
		exit 1; \
	fi;
